package com.example.wly.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

public class ColorCombinActivity extends AppCompatActivity {

    //成员
    int red = 0;//颜色分量，初始值=0
    int green = 0;
    int blue = 0;
    ImageView imColor;
    EditText etRed;
    EditText etGreen;
    EditText etBlue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_combin);
        //找到控件
        imColor = (ImageView) findViewById(R.id.im_color);
        etRed = (EditText) findViewById(R.id.et_red);
        etGreen = (EditText) findViewById(R.id.et_green);
        etBlue = (EditText) findViewById(R.id.et_blue);

        //监听文本数据改变
        etBlue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //改变完之后，处理代码
                //当文本输入框，输入完数据，获取每个输入框的数值
                String blueString = etBlue.getText().toString();//'100'
                if(blueString.equals("")) {
                    blue = 0;
                }else{
                    blue = Integer.parseInt(blueString);//将字符串转换成整形数
                    //判断范围
                    if(blue>255){
                        blue = 255;
                    }else if(blue<0){
                        blue = 0;
                    }
                }
                //根据数值合成color值，设置到Imageview
                imColor.setBackgroundColor(Color.rgb(red, green, blue));
            }
        });

        etRed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //改变前，处理代码

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //改变的时候，处理代码

            }

            @Override
            public void afterTextChanged(Editable s) {
                //改变完之后，处理代码
                //当文本输入框，输入完数据，获取每个输入框的数值
                String redString = etRed.getText().toString();//'100'
                if(redString.equals("")) {
                    red = 0;
                }else{
                    red = Integer.parseInt(redString);//将字符串转换成整形数
                    //判断范围
                    if(red>255){
                        red = 255;
                    }else if(red<0){
                        red = 0;
                    }
                }
                //根据数值合成color值，设置到Imageview
                imColor.setBackgroundColor(Color.rgb(red, green, blue));
            }
        });
        TextWatcher tw = new MyWatcher();//new TextWatcher()接口也不能new对象，因为含有抽象方法
        etGreen.addTextChangedListener(tw);

        //根据数值合成color值，设置到Imageview
    }
    //写一个子类实现接口的抽象方法
    class MyWatcher implements  TextWatcher {
      /*
        EditText etGeen;
        ImageView imColor;
        public MyWatcher(EditText t,ImageView i) {
            etGeen = t;
            imColor = i;
        }*/

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //改变前，处理代码

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //改变的时候，处理代码

        }

        @Override
        public void afterTextChanged(Editable s) {
            //改变完之后，处理代码
            //当文本输入框，输入完数据，获取每个输入框的数值
            String greenString = etGreen.getText().toString();//'100'
            if (greenString.equals("")) {
                green = 0;

            }else{
                green = Integer.parseInt(greenString);//将字符串转换成整形数
                //判断范围
                if(green>255){
                    green = 255;
                }else if(green<0){
                    green = 0;
                }
            }
            //根据数值合成color值，设置到Imageview
            imColor.setBackgroundColor(Color.rgb(red, green, blue));
        }
    }
}
